package com.ocom.webapi.controller;


import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.entity.master.EntityFileDownloadInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.payconfig.FeeDataListVo;
import com.ocom.common.vo.payconfig.PersonRefundVo;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.AccessClientService;
import com.ocom.webapi.feign.MqSubsidyClientService;
import com.ocom.webapi.feign.PayConfigClientService;
import com.ocom.webapi.service.FeeDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * fee_dataController
 *
 * @author junShu
 * @date 2023-08-18 16:57:26
 */
@Slf4j
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
@RestController
public class FeeDataController {

    @Resource
    private PayConfigClientService payConfigClientService;
    @Resource
    private FeeDataService feeDataService;
    @Resource
    private AccessClientService accessClientService;
    @Resource
    private MqSubsidyClientService maPayClientService;
    /**
     * 15.13缴费数据列表（feeData）
     */
    @PostMapping(WebApiServiceUrl.URL_FeeDetailOnShow)
    public Result feeDataList(@RequestBody FeeDataListVo request) {
//            Page<FeeData> page = feeDataService.selectFeeDataPage(pageNumber, pageSize);
        return payConfigClientService.feeDataList(request);
    }

    /**
     * 15.14增加缴费人员（addFeeData）
     */
    @PostMapping(WebApiServiceUrl.URL_FeeDetailAddPerson)
    public Result addFeeData(@RequestBody FeeDataListVo FeeData) {
        return payConfigClientService.addFeeData(FeeData);
    }

    /**
     * 15.15更改缴费金额（updateFeeMoney）
     */
    @PostMapping(WebApiServiceUrl.URL_FeeDetailModifyAmount)
    public Result updateFeeMoney(@RequestBody FeeDataListVo request) {
        return payConfigClientService.updateFeeMoney(request);
    }

    /**
     * 15.16删除缴费人员（removeFeeData）
     *
     * @param
     * @return
     */
    @PostMapping(WebApiServiceUrl.URL_FeeDetailDeletePerson)
    public Result deleteById(@RequestBody FeeDataListVo request) {
        return payConfigClientService.deleteById(request);
    }

    /**
     * 8.3.7导入缴费数据
     *
     * @param comId
     * @param feeItemId
     * @param file
     * @return
     */
    @PostMapping(WebApiServiceUrl.Url_FeeDetailImportData)
    public Result feeDetaiImportData(@RequestParam("comId") Long comId, @RequestParam("feeItemId")int feeItemId, @RequestPart("file") MultipartFile file) {
        log.info("导入缴费数据请求参数:{{}},{{}},{{}}", comId, feeItemId, file);
        return feeDataService.feeDetaiImportData(comId, feeItemId, file);
    }

    /**
     * 8.3.5导出缴费数据
     * @param request
     * @return
     */
    @PostMapping(WebApiServiceUrl.Url_FeeDetailExportData)
    public Result subsidyExportData(@RequestBody FeeDataListVo request) {
        EntityFileDownloadInfo entityFileDownloadInfo=new EntityFileDownloadInfo();
        YoCiUser yoCiUser= SecurityUtils.getYoCiUser();
        entityFileDownloadInfo.setPId(yoCiUser.getId());
//        entityFileDownloadInfo.setPId(34L);
        entityFileDownloadInfo.setName("导出缴费数据");
        entityFileDownloadInfo.setStatus(0l);
        Result result = accessClientService.SendExcelApply(entityFileDownloadInfo);
        if(result.getCode()!=200){
            return ResultUtil.error(ResultEnum.Err_4013);
        }
        request.setId((Integer)result.getData());
        request.setUserId(yoCiUser.getId());
        return maPayClientService.feeDataExport(request);
    }

    /**
     * 8.3.8批量退费
     * @param request
     * @return
     */
    @PostMapping(WebApiServiceUrl.Url_FeeDetailRefund)
    public Result personRefund(@Valid @RequestBody PersonRefundVo request){
        return payConfigClientService.personRefund(request);
    }

}
